package com.licheng.controller;

import com.licheng.anno.Log;
import com.licheng.pojo.Result;
import com.licheng.pojo.Student;
import com.licheng.pojo.StudentPageQuery;
import com.licheng.pojo.StudentPageResult;
import com.licheng.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 分页、条件查询学员
     * @param spq
     * @return
     */
    @GetMapping
    public Result selctStudentPage(StudentPageQuery spq) {
        StudentPageResult spr = studentService.selctStudentPage(spq);
        return Result.success(spr);
    }

    /**
     * 删除学生
     *
     * @param ids
     * @return
     */
    @Log
    @DeleteMapping("/{ids}")
    public Result deleteStudentById(@PathVariable List<Integer> ids) {
        studentService.deleteStudentById(ids);
        return Result.success();
    }

    /**
     * 添加学生
     * @param student
     * @return
     */
    @Log
    @PostMapping
    public Result insertStudent(@RequestBody Student student) {
        studentService.insertStudent(student);
        return Result.success();
    }

    /**
     * 根据id查询学生
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result selectStudentById(@PathVariable Integer id) {
        Student student = studentService.selectStudentById(id);
        return Result.success(student);
    }

    /**
     * 修改学生
     * @param student
     * @return
     */
    @Log
    @PutMapping
    public Result updateStudent(@RequestBody Student student) {
        studentService.updateStudent(student);
        return Result.success();
    }

    /**
     * 违纪扣分
     * @param id 学生id
     * @param score 扣除分数
     * @return
     */
    @Log
    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id, @PathVariable Integer score) {
        System.out.println("id = " + id);
        System.out.println("score = " + score);
        if (score == null) {
            return Result.error("没有输入违纪扣分！");
        }
        studentService.violation(id, score);
        return Result.success();
    }

}
